package com.aclass.xqx.classscreen;

import android.app.Application;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

import com.aclass.xqx.classscreen.db.DaoMaster;
import com.aclass.xqx.classscreen.db.DaoSession;
import com.aclass.xqx.classscreen.service.LocalService;
import com.aclass.xqx.classscreen.service.RemoteService;
import com.aclass.xqx.classscreen.utlis.AppUtlis;
import com.aclass.xqx.classscreen.utlis.Constant;
import com.aclass.xqx.classscreen.utlis.LocationUtlis;
import com.aclass.xqx.classscreen.utlis.PrefUtild;
import com.crashlytics.android.Crashlytics;

import java.io.File;

import io.fabric.sdk.android.Fabric;

/**
 * Created by xqx on 2017/10/26.
 *
 */

public class BaseApplication extends Application {

    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(restartHandler);
        Fabric.with(this, new Crashlytics());
        //配置数据库
        setupDatabase();
    }

    /**
     * 配置数据库
     */
    private void setupDatabase() {
        File destDir=new File(Environment.getExternalStorageDirectory().getPath()+"/Class");
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        //创建数据库
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, Environment.getExternalStorageDirectory().getPath()+"/Class/class.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }
    // 创建服务用于捕获崩溃异常
    private Thread.UncaughtExceptionHandler restartHandler = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            restartApp();//发生崩溃异常时,重启应用

        }
    };
    public void restartApp(){
        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());  //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
    }
    public static DaoSession getDaoInstant() {
        return daoSession;
    }
}